/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb.op;

/**
 * <p> JVM instruction <code>0xc4 wide</code>. </p>
 */
public final class Wide
    extends syntelos.xb.Op
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:wide");

    public final static class Opc
	extends syntelos.xb.Base.Attribute.Operand
    {
	public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ATTRIBUTE_NODE,"http://www.syntelos.org/xb/op","xop:wide.opc");

	public Opc(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
	    super(doc,type);
	}
	public Opc(org.w3c.dom.Document doc){
	    this(doc,NAME);
	}
	public Opc(){
	    this(DOC_NIL);
	}

	protected Wide getParentWide(){
	    return (Wide)this.getParentNode();
	}
	public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
	    return this.getParentWide().getAttributeLocalVariableTable();
	}
	public syntelos.xb.at.Code getAttributeCode(){
	    return this.getParentWide().getAttributeCode();
	}
	public syntelos.xb.op.Code getOpCode(){
	    return this.getParentWide().getOpCode();
	}
	public int getOperand(){
	    String mnemonic = this.getData();
	    int opcode = syntelos.xb.Op.FromString(mnemonic);
	    switch (opcode){
	    case Iload.code:
	    case Fload.code:
	    case Aload.code:
	    case Lload.code:
	    case Dload.code:
	    case Istore.code:
	    case Fstore.code:
	    case Astore.code:
	    case Lstore.code:
	    case Dstore.code:
	    case Ret.code:
	    case Iinc.code:
		return opcode;
	    default:
		throw new syntelos.xb.Error.Format(mnemonic);
	    }
	}
	public void setOperand(int opcode){
	    String mnemonic = syntelos.xb.Op.ToString(opcode);
	    switch (opcode){
	    case Iload.code:
	    case Fload.code:
	    case Aload.code:
	    case Lload.code:
	    case Dload.code:
	    case Istore.code:
	    case Fstore.code:
	    case Astore.code:
	    case Lstore.code:
	    case Dstore.code:
	    case Ret.code:
	    case Iinc.code:
		this.setData(mnemonic);
		return;
	    default:
		throw new syntelos.xb.Error.Format(mnemonic);
	    }
	}
	public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
	public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
    }

    public final static class Var
	extends syntelos.xb.Base.Attribute.Operand.Var
    {
	public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ATTRIBUTE_NODE,"http://www.syntelos.org/xb/op","xop:wide.var");

	public Var(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
	    super(doc,type);
	}
	public Var(org.w3c.dom.Document doc){
	    this(doc,NAME);
	}
	public Var(){
	    this(DOC_NIL);
	}

	protected Wide getParentWide(){
	    return (Wide)this.getParentNode();
	}
	public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
	    return this.getParentWide().getAttributeLocalVariableTable();
	}
	public syntelos.xb.at.Code getAttributeCode(){
	    return this.getParentWide().getAttributeCode();
	}
	public syntelos.xb.op.Code getOpCode(){
	    return this.getParentWide().getOpCode();
	}
	public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
	public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
    }

    public final static class Const
	extends syntelos.xb.Base.Attribute.Operand
    {
	public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ATTRIBUTE_NODE,"http://www.syntelos.org/xb/op","xop:wide.const");

	public Const(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
	    super(doc,type);
	}
	public Const(org.w3c.dom.Document doc){
	    this(doc,NAME);
	}
	public Const(){
	    this(DOC_NIL);
	}

	protected Wide getParentWide(){
	    return (Wide)this.getParentNode();
	}
	public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
	    return this.getParentWide().getAttributeLocalVariableTable();
	}
	public syntelos.xb.at.Code getAttributeCode(){
	    return this.getParentWide().getAttributeCode();
	}
	public syntelos.xb.op.Code getOpCode(){
	    return this.getParentWide().getOpCode();
	}
	public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
	public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
	    throws java.io.IOException
	{
	    throw new syntelos.xb.Error.Bug();	// Driven by Wide
	}
    }

    public final static int code = CODE_WIDE;
    public final static java.lang.String name = "wide";
    public final static int size_operands = 4;
    public final static int count_stackPop = 4;
    public final static int count_stack_push = 4;


    public Wide(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
        super(doc,type);
    }
    public Wide(){
        super(NAME,false);
    }
    public Wide(boolean shared){
        super(NAME,true);
    }

    public Opc getOperandOpcAttr(){
	return (Opc)this.getCreateAttributeNode(Opc.NAME);
    }
    public Var getOperandVarAttr(){
	return (Var)this.getCreateAttributeNode(Var.NAME);
    }
    public Const getOperandConstAttr(){
	return (Const)this.getCreateAttributeNode(Const.NAME);
    }
    public int getOperandOpc(){
	return this.getOperandOpcAttr().getOperand();
    }
    public void setOperandOpc(int value){
	this.getOperandOpcAttr().setOperand(value);
    }
    public int getOperandConst(){
	return this.getOperandConstAttr().getOperand();
    }
    public void setOperandConst(int value){
	this.getOperandConstAttr().setOperand(value);
    }
    public int getOperandVar(){
	return this.getOperandVarAttr().getOperand();
    }
    public void setOperandVar(int value){
	this.getOperandVarAttr().setOperand(value);
    }

    public int getCode(){
        return code;
    }
    public java.lang.String getName(){
        return name;
    }
    public int countOperands(){
	switch(this.getOperandOpc()){
	case Iload.code:
	case Fload.code:
	case Aload.code:
	case Lload.code:
	case Dload.code:
	case Istore.code:
	case Fstore.code:
	case Astore.code:
	case Lstore.code:
	case Dstore.code:
	case Ret.code:
	    return 1;

	case Iinc.code:
	    return 2;

	default:
	    return -1;
	}
    }
    public int sizeOperands(){
	switch(this.getOperandOpc()){
	case Iload.code:
	case Fload.code:
	case Aload.code:
	case Lload.code:
	case Dload.code:
	case Istore.code:
	case Fstore.code:
	case Astore.code:
	case Lstore.code:
	case Dstore.code:
	case Ret.code:
	    return 3;

	case Iinc.code:
	    return 5;

	default:
	    return size_operands;
	}
    }
    public int countStackPop(){
        return count_stackPop;
    }
    public int countStackPush(){
        return count_stack_push;
    }
    public void readCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Reader in)
        throws java.io.IOException
    {
        if (this.setPC(in)){
	    in.mark(java.lang.Integer.MAX_VALUE);
	    int operand_opcode = in.read();
	    this.setOperandOpc(operand_opcode);
	    switch (operand_opcode){
	    case Iload.code:
	    case Fload.code:
	    case Aload.code:
	    case Lload.code:
	    case Dload.code:
	    case Istore.code:
	    case Fstore.code:
	    case Astore.code:
	    case Lstore.code:
	    case Dstore.code:
	    case Ret.code:
		this.setOperandConst(in.readUnsignedShort());
		break;

	    case Iinc.code:
		this.setOperandConst(in.readUnsignedShort());
		this.setOperandVar(in.readUnsignedShort());
		break;
	    default:
		throw new syntelos.xb.Error.Format(syntelos.xb.Op.ToString(operand_opcode));
	    }

	    byte[] cf = in.marked();
	    this.setCF(cf);
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
    public void writeCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Writer out)
        throws java.io.IOException
    {
        if (this.setPC(out)){
	    byte[] cf = null;
	    int operand_opcode = this.getOperandOpc();
	    switch (operand_opcode){
	    case Iload.code:
	    case Fload.code:
	    case Aload.code:
	    case Lload.code:
	    case Dload.code:
	    case Istore.code:
	    case Fstore.code:
	    case Astore.code:
	    case Lstore.code:
	    case Dstore.code:
	    case Ret.code:
		cf = this.getCF(3);
		this.setCFIntU1(0,operand_opcode);
		this.setCFIntU2(1,this.getOperandConst());
		break;

	    case Iinc.code:
		cf = this.getCF(5);
		this.setCFIntU1(0,operand_opcode);
		this.setCFIntU2(1,this.getOperandConst());
		this.setCFIntU2(3,this.getOperandVar());
		break;
	    }
	    out.write(cf);
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
}
